package com.tem.product.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.tem.product.entity.CategoryAttribute;
import com.api.manage.pojo.product.CategoryAttributeBean;
import com.tem.product.mapper.CategoryAttributeMapper;
import com.tem.product.service.ICategoryAttributeService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 * 栏目属性 服务实现类
 * </p>
 *
 * @author 陈辉
 * @since 2023-11-08
 */
@Service
public class CategoryAttributeServiceImpl extends ServiceImpl<CategoryAttributeMapper, CategoryAttribute> implements ICategoryAttributeService {
    public List<CategoryAttributeBean> getByCategoryId(Long categoryId){
        QueryWrapper<CategoryAttribute> queryWrapper=new QueryWrapper<>();
        queryWrapper.lambda().eq(CategoryAttribute::getCategoryId,categoryId).orderByDesc(CategoryAttribute::getSeq);
        List<CategoryAttribute> list = this.list(queryWrapper);
        return toBeans(list);
    }

    public List<CategoryAttributeBean> getByCategoryIds(List<Long> categoryIds){
        QueryWrapper<CategoryAttribute> queryWrapper=new QueryWrapper<>();
        queryWrapper.lambda().in(CategoryAttribute::getCategoryId,categoryIds).orderByDesc(CategoryAttribute::getSeq);
        List<CategoryAttribute> list = this.list(queryWrapper);
        return toBeans(list);
    }

    private List<CategoryAttributeBean> toBeans(List<CategoryAttribute> list){
        if(list==null||list.size()<1)return null;
        List<CategoryAttributeBean> cbs = new ArrayList<>();
        CategoryAttributeBean bean = null;
        for(CategoryAttribute c:list){
            bean=new CategoryAttributeBean();
            BeanUtils.copyProperties(c,bean);
            cbs.add(bean);
        }
        return cbs;
    }
}
